package com.example.administrator.financialmanager;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.administrator.financialmanager.dao.InaccountDao;
import com.example.administrator.financialmanager.dao.Outaccountdao;
import com.example.administrator.financialmanager.model.Tb_inaccount;
import com.example.administrator.financialmanager.model.Tb_outaccount;
import java.util.Calendar;

public class IncomeDetailActivity extends AppCompatActivity {
    protected static final int DATE_DIALOG_ID = 0;// 创建日期对话框常量
    EditText detail_income_money_input, detail_income_time_input, detail_income_payer_input, detail_income_describe_input;// 创建4个EditText对象
    Spinner detail_income_kind_input;// 创建Spinner对象
    Button detail_income_admit, detail_income_delete,detail_income_back;// 创建两个Button对象
    String[] strInfos;// 定义字符串数组
    String strid, strType;// 定义两个字符串变量，分别用来记录信息编号和管理类型

    private int mYear;// 年
    private int mMonth;// 月
    private int mDay;// 日

    InaccountDao inaccountDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_income_detail);// 设置布局文件

        inaccountDao = new InaccountDao(IncomeDetailActivity.this);// 创建InaccountDAO对象


        detail_income_money_input = (EditText) findViewById(R.id.detail_income_money_input);// 获取金额文本框
        detail_income_time_input = (EditText) findViewById(R.id.detail_income_time_input);// 获取时间文本框
        detail_income_kind_input = (Spinner) findViewById(R.id.detail_income_kind_input);// 获取类别下拉列表
        detail_income_payer_input = (EditText) findViewById(R.id.detail_income_payer_input);// 获取地点/付款方文本框
        detail_income_describe_input = (EditText) findViewById(R.id.detail_income_describe_input);// 获取备注文本框
        detail_income_admit = (Button) findViewById(R.id.detail_income_admit);// 获取修改按钮
        detail_income_delete = (Button) findViewById(R.id.detail_income_delete);// 获取删除按钮
        detail_income_back = (Button) findViewById(R.id.detail_income_back);// 获取删除按钮

        Intent intent = getIntent();// 创建Intent对象
//        Bundle bundle = intent.getExtras();// 获取传入的数据，并使用Bundle记录
//        strInfos = bundle.getStringArray(IncomeListActivity.FLAG);// 获取Bundle中记录的信息
        strInfos = intent.getStringArrayExtra(IncomeListActivity.FLAG);// 获取Bundle中记录的信息
        strid = strInfos[0];// 记录id
        strType = strInfos[1];// 记录类型

        // 根据编号查找收入信息，并存储到Tb_outaccount对象中
        Tb_inaccount tb_inaccount = inaccountDao.find(strid);
        detail_income_money_input.setText(String.valueOf(tb_inaccount.getMoney()));// 显示金额
        detail_income_time_input.setText(tb_inaccount.getTime());// 显示时间
        ///******************修改下拉列表项***********************
        //创建一个适配器
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                IncomeDetailActivity.this, R.array.intype,android.R.layout.simple_dropdown_item_1line);
        detail_income_kind_input.setAdapter(adapter); 					// 将适配器与选择列表框关联
        detail_income_kind_input.setPrompt(tb_inaccount.getType());// 显示类别
        detail_income_payer_input.setText(tb_inaccount.getHandler());// 显示付款方
        detail_income_describe_input.setText(tb_inaccount.getMark());// 显示备注\


        detail_income_time_input.setOnClickListener(new View.OnClickListener() {// 为时间文本框设置单击监听事件
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                showDialog(DATE_DIALOG_ID);// 显示日期选择对话框
            }
        });

        detail_income_admit.setOnClickListener(new View.OnClickListener() {// 为修改按钮设置监听事件
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                Tb_inaccount tb_inaccount = new Tb_inaccount();// 创建Tb_inaccount对象
                tb_inaccount.set_id(strid);// 设置编号
                tb_inaccount.setMoney(Double.parseDouble(detail_income_money_input.getText()
                        .toString()));// 设置金额
                tb_inaccount.setTime(detail_income_time_input.getText().toString());// 设置时间
                tb_inaccount.setType(detail_income_kind_input.getSelectedItem().toString());// 设置类别
                tb_inaccount.setHandler(detail_income_payer_input.getText().toString());// 设置付款方
                tb_inaccount.setMark(detail_income_describe_input.getText().toString());// 设置备注
                inaccountDao.update(tb_inaccount);// 更新收入信息
                // 弹出信息提示
                Toast.makeText(IncomeDetailActivity.this, "〖数据〗修改成功！", Toast.LENGTH_SHORT)
                        .show();
            }
        });

        detail_income_delete.setOnClickListener(new View.OnClickListener() {// 为删除按钮设置监听事件
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
              inaccountDao.detele(strid);// 根据编号删除收入信息

                Toast.makeText(IncomeDetailActivity.this, "〖数据〗删除成功！", Toast.LENGTH_SHORT)
                        .show();
            }
        });
        detail_income_back.setOnClickListener(new View.OnClickListener() {// 为删除按钮设置监听事件
            @Override
            public void onClick(View arg0) {
                finish();
            }
        });

        final Calendar c = Calendar.getInstance();// 获取当前系统日期
        mYear = c.get(Calendar.YEAR);// 获取年份
        mMonth = c.get(Calendar.MONTH);// 获取月份
        mDay = c.get(Calendar.DAY_OF_MONTH);// 获取天数
        updateDisplay();// 显示当前系统时间
    }

    @Override
    protected Dialog onCreateDialog(int id){// 重写onCreateDialog方法
        switch (id) {
            case DATE_DIALOG_ID:// 弹出日期选择对话框
                return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
                        mDay);
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            mYear = year;// 为年份赋值
            mMonth = monthOfYear;// 为月份赋值
            mDay = dayOfMonth;// 为天赋值
            updateDisplay();// 显示设置的日期
        }
    };

    private void updateDisplay() {
        // 显示设置的时间
        detail_income_time_input.setText(new StringBuilder().append(mYear).append("-")
                .append(mMonth + 1).append("-").append(mDay));
    }
}
